package com.zkyc.framework.file.util;

import io.minio.MinioClient;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * minio客户端单例类
 */
@Getter
@Component
public class MinioClientSingleton {

    private static String domainUrl;
    private static String accessKey;
    private static String secretKey;
    @Value("${minio.url}")
    private String domain_url;
    @Value("${minio.accessKey}")
    private String access_key;
    @Value("${minio.secretKey}")
    private String secret_key;

    @PostConstruct
    public void getConfiguration() {
        domainUrl = this.domain_url;
        accessKey = this.access_key;
        secretKey = this.secret_key;
    }

    private volatile static MinioClient minioClient;

    public MinioClientSingleton() {
    }

    /**
     * 获取minio客户端实例
     *
     * @return {@link MinioClient}
     */
    public static MinioClient getMinioClient() {
        if (minioClient == null) {
            synchronized (MinioClientSingleton.class) {
                if (minioClient == null) {
                    minioClient = MinioClient.builder()
                            .endpoint(domainUrl)
                            .credentials(accessKey, secretKey)
                            .build();
                }
            }
        }
        return minioClient;
    }
}